package com.ruoyi.web.controller.riskAssessment;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.riskAssessment.dto.RiskIdentifyEvaluationDto;
import com.ruoyi.riskAssessment.vo.RiskIdentifyEvaluationVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.riskAssessment.domain.RiskIdentifyEvaluation;
import com.ruoyi.riskAssessment.service.IRiskIdentifyEvaluationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 危险源-识别评估Controller
 *
 * @author lth
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/riskAssessment/evaluation")
public class RiskIdentifyEvaluationController extends BaseController
{
    @Autowired
    private IRiskIdentifyEvaluationService riskIdentifyEvaluationService;

    /**
     * 查询危险源-识别评估列表
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:evaluation:list')")
    @GetMapping("/list")
    public TableDataInfo list(RiskIdentifyEvaluation riskIdentifyEvaluation)
    {
        startPage();
        List<RiskIdentifyEvaluation> list = riskIdentifyEvaluationService.selectRiskIdentifyEvaluationList(riskIdentifyEvaluation);
        return getDataTable(list);
    }

    /**
     * 导出危险源-识别评估列表
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:evaluation:export')")
    @Log(title = "危险源-识别评估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RiskIdentifyEvaluation riskIdentifyEvaluation)
    {
        List<RiskIdentifyEvaluation> list = riskIdentifyEvaluationService.selectRiskIdentifyEvaluationList(riskIdentifyEvaluation);
        ExcelUtil<RiskIdentifyEvaluation> util = new ExcelUtil<RiskIdentifyEvaluation>(RiskIdentifyEvaluation.class);
        util.exportExcel(response, list, "危险源-识别评估数据");
    }

    /**
     * 获取危险源-识别评估详细信息
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:evaluation:query')")
    @GetMapping(value = "/{riskIdentifyEvaluationId}")
    public AjaxResult getInfo(@PathVariable("riskIdentifyEvaluationId") String riskIdentifyEvaluationId)
    {
        return success(riskIdentifyEvaluationService.selectRiskIdentifyEvaluationByRiskIdentifyEvaluationId(riskIdentifyEvaluationId));
    }

    /**
     * 新增危险源-识别评估
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:evaluation:add')")
    @Log(title = "危险源-识别评估", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RiskIdentifyEvaluation riskIdentifyEvaluation)
    {
        Date date = new Date();
        riskIdentifyEvaluation.setReportAt(date);
        String s = "PG";
        String r = generateRandomString(3);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd");
        String currentDate = LocalDate.now().format(formatter);
        String id = s + currentDate + r;

        riskIdentifyEvaluation.setReportStatus("0");
        riskIdentifyEvaluation.setAssessmentLevel("0");
        riskIdentifyEvaluation.setRiskIdentifyEvaluationId(id);
        riskIdentifyEvaluation.setRiskCode(id);
        return toAjax(riskIdentifyEvaluationService.insertRiskIdentifyEvaluation(riskIdentifyEvaluation));
    }

    private static String generateRandomString(int length) {
        String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
        StringBuilder sb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            char c = chars.charAt(random.nextInt(chars.length()));
            sb.append(c);
        }
        return sb.toString();
    }

    /**
     * 修改危险源-识别评估
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:evaluation:edit')")
    @Log(title = "危险源-识别评估", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RiskIdentifyEvaluation riskIdentifyEvaluation)
    {
        return toAjax(riskIdentifyEvaluationService.updateRiskIdentifyEvaluation(riskIdentifyEvaluation));
    }

    /**
     * 删除危险源-识别评估
     */
    @PreAuthorize("@ss.hasPermi('riskAssessment:evaluation:remove')")
    @Log(title = "危险源-识别评估", businessType = BusinessType.DELETE)
	@DeleteMapping("/{riskIdentifyEvaluationIds}")
    public AjaxResult remove(@PathVariable String[] riskIdentifyEvaluationIds)
    {
        return toAjax(riskIdentifyEvaluationService.deleteRiskIdentifyEvaluationByRiskIdentifyEvaluationIds(riskIdentifyEvaluationIds));
    }


    /**     待上报/已上报危险源列表
    * @Author lth
    * @Description //TODO
    * @Date 9:48 2023/12/29
    * @Param
    * @return
     **/
    @PreAuthorize("@ss.hasPermi('riskAssessment:evaluation:query')")
    @GetMapping("/riskWaitReportList")
    public TableDataInfo riskWaitReportList(RiskIdentifyEvaluationVo vo)
    {
        startPage();
        PageInfo<RiskIdentifyEvaluationDto> list = riskIdentifyEvaluationService.riskWaitReportList(vo);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list.getList());
        rspData.setTotal(list.getTotal());
        return rspData;
    }
}
